<template>
  <div class="app-container">
    <label class="title" style="font-size: 20px;color: rgba(0,0,0,.85);font-weight: 600;line-height: 32px">考点管理</label>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="关键字" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="关键字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="考试项目" prop="ruleStatus">
        <el-select v-model="queryParams.projectId" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in projectIdList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:examPlace:add']"
        >添加考点
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="placeList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{scope.$index+(queryParams.pageNo-1)*queryParams.pageSize+1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="考点编号" align="center" prop="code"/>
      <el-table-column label="考点名称" align="center" prop="name"/>
      <el-table-column label="考点开始时间" align="center" prop="gmtStarted" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtStarted, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考点结束时间" align="center" prop="gmtEnded" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtEnded, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考试项目" align="center" prop="project"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:examPlace:edit']"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:examPlace:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改考点对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="考点编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="考点名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="考试日期" prop="datetime">
          <el-date-picker
            v-model="form.datetime"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="考试项目" prop="projectIds">
          <el-checkbox-group v-model="form.projectIds">
            <el-checkbox
              v-for="country in projectIdList"
              :label="country.id"
              style="width: 150px"
            >{{country.name}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import RuleInfo from './ruleInfo'
  import { delPlace, examPlaces, getById, listPlace, updatePlaces } from '@/api/business/place/place'
  import { examProjectsList } from '@/api/business/candidate/candidate'

  export default {
    name: 'Place',
    components: {
      RuleInfo
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 考点表格数据
        placeList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        openRule: false,
        // 查询参数
        queryParams: {
          pageNo: 1,
          pageSize: 10,
          code: null,
          name: null,
          gmtStarted: null,
          gmtEnded: null,
          ruleStatus: null,
          gmtCreated: null,
          gmtModified: null,
          isDeleted: null
        },
        // 表单参数
        form: {
          projectIds: []
        },
        // 表单校验
        rules: {
          code: [
            { required: true, message: '考点代码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '考点名称不能为空', trigger: 'blur' }
          ],
          datetime: [
            { required: true, message: '考试日期不能为空', trigger: 'blur' }
          ]
        },
        projectIdList: [],
        updateId: null,
        saveType: 'add',
        editableTabs: [],
        editableTabsValue: ''
      }
    },
    created() {
      this.getList()
      examProjectsList().then(response => {
        this.projectIdList = response.data
      })
    },
    methods: {
      /** 查询考点列表 */
      getList() {
        this.loading = true
        listPlace(this.queryParams).then(response => {
          this.placeList = response.data.records
          this.total = response.data.totalCount
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          code: null,
          name: null,
          datetime: null,
          remark: null,
          projectIds: []
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加考点'
        this.saveType = 'add'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getById(id).then(response => {
          const data = response.data
          this.form.code = data.code
          this.form.name = data.name
          this.form.datetime = [data.gmtStarted, data.gmtEnded]
          this.form.projectIds = data.projectIds
          this.form.remark = data.remark
          this.updateId = data.id

          this.open = true
          this.title = '修改考点'
          this.saveType = 'update'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            const datatime = this.form.datetime
            this.form.gmtStarted = datatime[0]
            this.form.gmtEnded = datatime[1]
            if (this.saveType === 'update') {
              updatePlaces(this.form, this.updateId).then(response => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              examPlaces(this.form).then(response => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$modal.confirm('是否确认删除考点为"' + row.name + '"的数据项？').then(function() {
          return delPlace(ids)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        }).catch(() => {
        })
      }
    }
  }
</script>
